package bb.lanxing.util;

import android.location.Location;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;

import java.util.ArrayList;
import java.util.List;

public class BiCiCoorConverter {
    static double a = 6378245.0d;
    static CoordinateConverter converter = new CoordinateConverter();
    static double ee = 0.006693421622965943d;
    static double x_pi = 52.35987755982988d;

    public static LatLng baidu2Earth(LatLng latLng) {
        return !LocCountry.inChina(latLng, true) ? latLng : common2Earth(baidu2Common(latLng));
    }

    public static LatLng baidu2Common(LatLng latLng) {
        if (!LocCountry.inChina(latLng, true)) {
            return latLng;
        }
        double d = latLng.longitude - 0.0065d;
        double d2 = latLng.latitude - 0.006d;
        double sqrt = Math.sqrt((d * d) + (d2 * d2)) - (Math.sin(x_pi * d2) * 2.0E-5d);
        double atan2 = Math.atan2(d2, d) - (Math.cos(d * x_pi) * 3.0E-6d);
        LatLng latLng2 = new LatLng(sqrt * Math.sin(atan2), Math.cos(atan2) * sqrt);
        return LocCountry.inTW_HK(latLng) ? common2EarthDirectly(latLng2) : latLng2;
    }

    public static LatLng earth2Baidu(LatLng latLng) {
        if (!LocCountry.inChina(latLng, true)) {
            return latLng;
        }
        converter.coord(latLng);
        converter.from(CoordinateConverter.CoordType.GPS);
        return converter.convert();
    }

    public static LatLng common2Baidu(LatLng latLng) {
        if (!LocCountry.inChina(latLng, true)) {
            return latLng;
        }
        converter.coord(latLng);
        converter.from(LocCountry.inTW_HK(latLng) ? CoordinateConverter.CoordType.GPS : CoordinateConverter.CoordType.COMMON);
        return converter.convert();
    }

    public static BDLocation common2Baidu(BDLocation bDLocation) {
        LatLng latLng = new LatLng(bDLocation.getLatitude(), bDLocation.getLongitude());
        converter.coord(latLng);
        converter.from(LocCountry.inTW_HK(latLng) ? CoordinateConverter.CoordType.GPS : CoordinateConverter.CoordType.COMMON);
        LatLng convert = converter.convert();
        bDLocation.setLatitude(convert.latitude);
        bDLocation.setLongitude(convert.longitude);
        return bDLocation;
    }

    public static Location earth2Baidu(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CoordinateConverter coordinateConverter = new CoordinateConverter();
        coordinateConverter.coord(latLng);
        coordinateConverter.from(CoordinateConverter.CoordType.GPS);
        LatLng convert = coordinateConverter.convert();
        location.setLatitude(convert.latitude);
        location.setLongitude(convert.longitude);
        return location;
    }

    public static LatLng common2Earth(LatLng latLng) {
        return !LocCountry.inChina(latLng, false) ? latLng : common2EarthDirectly(latLng);
    }

    public static LatLng common2EarthDirectly(LatLng latLng) {
        double d = latLng.latitude;
        double d2 = latLng.longitude;
        double d3 = d2 - 105.0d;
        double d4 = d - 35.0d;
        double transformLat = transformLat(d3, d4);
        double transformLon = transformLon(d3, d4);
        double d5 = (d / 180.0d) * 3.141592653589793d;
        double sin = Math.sin(d5);
        double d6 = 1.0d - ((ee * sin) * sin);
        double sqrt = Math.sqrt(d6);
        return new LatLng(d - ((transformLat * 180.0d) / ((((1.0d - ee) * a) / (d6 * sqrt)) * 3.141592653589793d)), d2 - ((transformLon * 180.0d) / (((a / sqrt) * Math.cos(d5)) * 3.141592653589793d)));
    }

    private static LatLng earth2CommonDirectly(LatLng latLng) {
        double latitude = latLng.latitude;
        double longitude = latLng.longitude;
        double d3 = longitude - 105.0d;
        double d4 = latitude - 35.0d;
        double transformLat = transformLat(d3, d4);
        double transformLon = transformLon(d3, d4);
        double d5 = (latitude / 180.0d) * 3.141592653589793d;
        double sin = Math.sin(d5);
        double d6 = 1.0d - ((ee * sin) * sin);
        double sqrt = Math.sqrt(d6);
        return new LatLng(latitude + ((transformLat * 180.0d) / ((((1.0d - ee) * a) / (d6 * sqrt)) * 3.141592653589793d)), longitude + ((transformLon * 180.0d) / (((a / sqrt) * Math.cos(d5)) * 3.141592653589793d)));
    }

    public static LatLng earth2Common(LatLng latLng) {
        return LocCountry.inChina(latLng, false) ? earth2CommonDirectly(latLng) : latLng;
    }

    public static List<LatLng> earth2Common(List<LatLng> list) {
        if (LocCountry.inChina(list.get(list.size() / 2), false)) {
            ArrayList<LatLng> arrayList = new ArrayList<>();
            for (LatLng latLng : list) {
                arrayList.add(earth2CommonDirectly(latLng));
            }
            return arrayList;
        }
        return list;
    }

    static double transformLat(double d, double d2) {
        double d3 = d * 2.0d;
        double d4 = d2 * 3.141592653589793d;
        return (-100.0d) + d3 + (d2 * 3.0d) + (d2 * 0.2d * d2) + (0.1d * d * d2) + (Math.sqrt(Math.abs(d)) * 0.2d) + ((((Math.sin((6.0d * d) * 3.141592653589793d) * 20.0d) + (Math.sin(d3 * 3.141592653589793d) * 20.0d)) * 2.0d) / 3.0d) + ((((Math.sin(d4) * 20.0d) + (Math.sin((d2 / 3.0d) * 3.141592653589793d) * 40.0d)) * 2.0d) / 3.0d) + ((((Math.sin((d2 / 12.0d) * 3.141592653589793d) * 160.0d) + (Math.sin(d4 / 30.0d) * 320.0d)) * 2.0d) / 3.0d);
    }

    static double transformLon(double d, double d2) {
        double d3 = d * 0.1d;
        return d + 300.0d + (d2 * 2.0d) + (d3 * d) + (d3 * d2) + (Math.sqrt(Math.abs(d)) * 0.1d) + ((((Math.sin((6.0d * d) * 3.141592653589793d) * 20.0d) + (Math.sin((d * 2.0d) * 3.141592653589793d) * 20.0d)) * 2.0d) / 3.0d) + ((((Math.sin(d * 3.141592653589793d) * 20.0d) + (Math.sin((d / 3.0d) * 3.141592653589793d) * 40.0d)) * 2.0d) / 3.0d) + ((((Math.sin((d / 12.0d) * 3.141592653589793d) * 150.0d) + (Math.sin((d / 30.0d) * 3.141592653589793d) * 300.0d)) * 2.0d) / 3.0d);
    }
}